package com.ikun.controller;

import cn.hutool.core.util.StrUtil;
import com.ikun.core.annotation.MethodLog;
import com.ikun.core.utils.R;
import com.ikun.pojo.dto.UserDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

@Slf4j
@Tag(name = "测试接口", description = "做一些测试")
@RestController
@Validated
@RequestMapping("/test")
public class TestController {

    @Operation(summary = "get 不使用注解，DTO接参是怎么样的？", description = "答案是 dto 以 query 字符串传参")
    @MethodLog("xxx接口")
    @GetMapping("/dto/get")
    public R<Object> dtoget(@Validated UserDTO dto) {
        log.info("dtoget:{}", dto);
//        延迟 5s 返回
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return R.success(dto);
    }

    @Operation(summary = "post 不使用注解，DTO接参是怎么样的？",
            description = "答案是 dto 以 query 字符串或者 body x-www-form-urlencoded 格式都可以，两个同时传也可以")
    @PostMapping("/dto/post")
    public R<Object> dtopost(@Validated UserDTO dto) {
        log.info("dtopost:{}", dto);
        return R.success(dto);
    }

    @Operation(summary = "get DTO接收参数用 @RequestParam 到底行不行？", description = "答案是不行，dto 会被当做 query 参数")
    @GetMapping("/dto1/get")
    public R<Object> dto1get(@RequestParam UserDTO dto) {
        log.info("dto1get:{}", dto);
        return R.success(dto);
    }

    @Operation(summary = "post DTO接收参数用 @RequestParam 到底行不行？", description = "答案是不行，dto 会被当做 query 参数")
    @PostMapping("/dto1/post")
    public R<Object> dto1post(@RequestParam UserDTO dto) {
        log.info("dto1post:{}", dto);
        return R.success(dto);
    }

    @Operation(summary = "get DTO接收参数写 @ModelAttribute 和不写注解的情况表现是不是相同",
            description = "答案是和不写注解的情况完全一样")
    @GetMapping("/dto2/get")
    public R<Object> dto2get(@ModelAttribute UserDTO dto) {
        log.info("dto2get:{}", dto);
        return R.success(dto);
    }

    @Operation(summary = "post DTO接收参数写 @ModelAttribute 和不写注解的情况表现是不是相同",
            description = "答案是和不写注解的情况完全一样")
    @PostMapping("/dto2/post")
    public R<Object> dto2post(@Validated @ModelAttribute UserDTO dto) {
        log.info("dto2post:{}", dto);
        return R.success(dto);
    }

    @Operation(summary = "get 不写注解平铺参数用是怎么接参的？", description = "答案是以 query 字符串接参")
    @GetMapping("/query/get")
    public R<Object> queryget(@NotNull String username, Integer age) {
        log.info("queryget:{},{}", username, age);
        return R.success(StrUtil.format("username:{},age:{}", username, age));
    }

    @Operation(summary = "post 不写注解平铺参数用是怎么接参的？body x-www-form-urlencoded 格式传参是否会赋值给平铺的参数？",
            description = "答案是和 dto 一样，以 query 字符串或者 body x-www-form-urlencoded 格式都可以，两个同时传也可以。前端参数会赋值给对应的平铺参数")
    @PostMapping("/query/post")
    public R<Object> querypost(String username, Integer age) {
        log.info("querypost:{},{}", username, age);
        return R.success(StrUtil.format("username:{},age:{}", username, age));
    }

    @Operation(summary = "get 用 @RequestParam 注解平铺参数用是怎么接参的？", description = "答案是和不写注解的情况完全一样")
    @GetMapping("/query1/get")
    public R<Object> query1get(@NotNull @RequestParam String username, @RequestParam Integer age) {
        log.info("query1get:{},{}", username, age);
        return R.success(StrUtil.format("username:{},age:{}", username, age));
    }

    @Operation(summary = "post 用 @RequestParam 注解平铺参数用是怎么接参的？？",
            description = "答案是和不写注解的情况完全一样。由此也可知，@RequestParam 注解只作用于平铺的一般参数，并且它确实支持 body x-www-form-urlencoded 格式传参。")
    @PostMapping("/query1/post")
    public R<Object> query1post(@RequestParam String username, @RequestParam Integer age) {
        log.info("query1post:{},{}", username, age);
        return R.success(StrUtil.format("username:{},age:{}", username, age));
    }

}
